package sit.engreal.leetcode.problems200;

import org.junit.jupiter.api.Assertions;

/**
 * <a href="https://leetcode.cn/problems/reverse-bits/">190. 颠倒二进制位</a>
 */
public class LeetCode190 {
    // you need treat n as an unsigned value
    public int reverseBits(int n) {
        int m = 0;
        for (int i = 0; i < Integer.SIZE; i++) {
            m <<= 1;
            m |= (n & 1);
            n >>>= 1;
        }
        return m;
    }

    public static void main(String[] args) {
        final LeetCode190 demo = new LeetCode190();
        Assertions.assertEquals(0b10111111111111111111111111111111,
                demo.reverseBits(0b11111111111111111111111111111101));
        Assertions.assertEquals(0, demo.reverseBits(0));
    }
}
